﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Windows.Media.Animation;
using DiasEngineDate.UI;
using Microsoft.Win32;
using System.Windows.Resources;

namespace DiasEngineDate
{
    /// <summary>
    /// Main.xaml 的交互逻辑
    /// </summary>
    public partial class TheMainWin : Window
    {
        /*
        * 进度时间设置1
        */
        private TimeSpan duration;
        private DispatcherTimer timer = new DispatcherTimer();
        private OpenFileDialog videoDialog = new OpenFileDialog();
        private bool isPlaying = false;
        /*
       * 进度时间设置1
       */

        //鼠标移除10秒后隐藏视频控制按钮
        DispatcherTimer timervideocontrol = new DispatcherTimer();



        //引用配置文件库
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        FunctionINC inc = new FunctionINC();

        public TheMainWin()
        {
            InitializeComponent();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Application.Current.Shutdown(-1);
            Environment.Exit(0);
        }


        //变量，记录窗口目前最大化或默认状态
        bool ismax = false;
        bool menushow = true;
        bool configshow = false;
        bool movieshow = false;

        MainContents msc;
        //页面按钮方法
        private void TheButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender as Button;
            if (btn.Name == "close_btn")//关闭按钮
            {
                this.Close();
            }
            else if (btn.Name == "max_btn")//最大化
            {
                if (ismax)
                {
                    this.WindowState = WindowState.Normal;//默认窗口
                    ismax = false;
                }
                else
                {
                    this.WindowState = WindowState.Maximized;//最大化窗口
                    ismax = true;
                }
            }
            else if (btn.Name == "mix_btn")//最小化
            {
                this.WindowState = WindowState.Minimized;
            }
            else if (btn.Name == "skin_btn")//更换背景
            {
                //使用线程启动背元素
                Thread settbg = new Thread(new ThreadStart(SettingBackground));
                //settbg.SetApartmentState(ApartmentState.STA);
                settbg.Start();
            }
            else if (btn.Name == "CenterBtn")//隐藏或打开菜单栏
            {
                ShowHiddenMenu();
                ////隐藏
                //DoubleAnimation dfrom = new DoubleAnimation();
                //dfrom.From = 200;
                //dfrom.To = 0;
                //dfrom.Duration = TimeSpan.FromSeconds(0.2);
                //DoubleAnimation dfromo = new DoubleAnimation();
                //dfromo.From = 1;
                //dfromo.To = 0;
                //dfromo.Duration = TimeSpan.FromSeconds(0.1);

                ////显示
                //DoubleAnimation dto = new DoubleAnimation();
                //dto.From = 0;
                //dto.To = 200;
                //dto.Duration = TimeSpan.FromSeconds(0.2);
                //DoubleAnimation dtoo = new DoubleAnimation();
                //dtoo.From = 0;
                //dtoo.To = 1;
                //dtoo.Duration = TimeSpan.FromSeconds(0.1);


                ////logo显示
                //DoubleAnimation dalogo = new DoubleAnimation();
                //dalogo.From = 0;
                //dalogo.To = 200;
                //dalogo.Duration = TimeSpan.FromSeconds(0.2);
                //DoubleAnimation dalogoo = new DoubleAnimation();
                //dalogoo.From = 0;
                //dalogoo.To = 1;
                //dalogoo.Duration = TimeSpan.FromSeconds(0.1);

                ////logo隐藏
                //DoubleAnimation dalogohid = new DoubleAnimation();
                //dalogohid.From = 200;
                //dalogohid.To = 0;
                //dalogohid.Duration = TimeSpan.FromSeconds(0.2);
                //DoubleAnimation dalogohido = new DoubleAnimation();
                //dalogohido.From = 1;
                //dalogohido.To = 0;
                //dalogohido.Duration = TimeSpan.FromSeconds(0.1);

                //if (menushow)
                //{
                //    LeftMenu.BeginAnimation(FrameworkElement.WidthProperty,dfrom);
                //    LeftMenu.BeginAnimation(FrameworkElement.OpacityProperty,dfromo);
                //    test.Width = (System.Windows.GridLength.Auto);

                //    //logo
                //    logobg.BeginAnimation(FrameworkElement.WidthProperty, dalogohid);
                //    logobg.BeginAnimation(FrameworkElement.OpacityProperty, dalogohido);

                //    menushow = false;
                //}
                //else
                //{
                //    LeftMenu.BeginAnimation(FrameworkElement.WidthProperty,dto);
                //    LeftMenu.BeginAnimation(FrameworkElement.OpacityProperty,dtoo);
                //    test.Width = System.Windows.GridLength.Auto;

                //    //logo
                //    logobg.BeginAnimation(FrameworkElement.WidthProperty, dalogo);
                //    logobg.BeginAnimation(FrameworkElement.OpacityProperty, dalogoo);


                //    menushow = true;
                //}

            }
            else if (btn.Name == "SystemInfos")//打开系统信息
            {
                MainContents.Visibility = System.Windows.Visibility.Visible;
                Home.Visibility = System.Windows.Visibility.Hidden;

                SystemInfos sis = new SystemInfos(Home, MainContents, RightContent);
                msc = new MainContents(sis);
                MainContents.Child = msc;
            }
            else if (btn.Name == "ConfigControl")//打开控制面板
            {
                ConfigBox.Visibility = System.Windows.Visibility.Visible;
                configshow = true;
            }
            else if (btn.Name == "closeconfig")//关闭控制面板
            {
                ConfigBox.Visibility = System.Windows.Visibility.Hidden;
                configshow = false;
            }
            else if (btn.Name == "MovieCtrl")//视频控制面板
            {
                MovieBox.Visibility = System.Windows.Visibility.Visible;
                //MovieControl.Visibility = System.Windows.Visibility.Visible;
                movieshow = true;
                this.timer.Start();//启动让进度条显示播放进度
                video.Play();
            }
            else if (btn.Name == "PLast")//快退
            {
                video.Position = this.video.Position - TimeSpan.FromSeconds(20);
            }
            else if (btn.Name == "PNext")//快进
            {
                video.Position = video.Position + TimeSpan.FromSeconds(20);
            }
            else if (btn.Name == "PPlay")//播放
            {
                video.Play();
            }
            else if (btn.Name == "PStop")//停止
            {
                video.Stop();
            }
            else if (btn.Name == "PPause")//暂停
            {
                video.Pause();
            }
            else if (btn.Name == "CloseMovie")//关闭视频控件框
            {
                MovieBox.Visibility = System.Windows.Visibility.Hidden;
                MovieControl.Visibility = System.Windows.Visibility.Hidden;
                movieshow = false;
                this.timer.Stop();//停止时间记录。减少资源损耗
                video.Pause();
            }
            else if (btn.Name == "Rolebtn")//打开角色面板
            {
                MainContents.Visibility = System.Windows.Visibility.Visible;
                Home.Visibility = System.Windows.Visibility.Hidden;

                Role r = new Role(Home, MainContents);
                msc = new MainContents(r);
                MainContents.Child = msc;
            }
            else if (btn.Name == "Rightsbtn")//打开权限面板
            {
                MainContents.Visibility = System.Windows.Visibility.Visible;
                Home.Visibility = System.Windows.Visibility.Hidden;

                Rights r = new Rights(Home, MainContents, RightContent);
                msc = new MainContents(r);
                MainContents.Child = msc;
            }
            else if (btn.Name == "Toolsbtn")//打开工具版面
            {
                MainContents.Visibility = System.Windows.Visibility.Visible;
                Home.Visibility = System.Windows.Visibility.Hidden;

                Tools t = new Tools(Home, MainContents, RightContent);
                msc = new MainContents(t);
                MainContents.Child = msc;
            }
        }

        //选择背景
        private void SettingBackground()
        {
            this.video.Dispatcher.Invoke(new Action(() =>
            {
                inc.SettingVideo(this.video, Login.SEname);
            }));

        }

        //当正常打开背景图片
        private void video_MediaOpened(object sender, RoutedEventArgs e)
        {

            if (video.Source.ToString() != "")
            {
                if (inc.isImg(video.Source.ToString()))
                {

                }
                else
                {
                    try
                    {
                        //视频总时长
                        double seconds = video.NaturalDuration.TimeSpan.TotalSeconds;
                        //共分成10等分，每1等分多少秒
                        Postionloading.Maximum = seconds / 10;
                        //10分之一
                        double baseSecond = seconds / Postionloading.Maximum;
                        this.timer.Interval = new TimeSpan(0, 0, 1);
                        this.timer.Tick += new EventHandler(timer_Tick);
                    }
                    catch (Exception)
                    {

                    }
                }
            }
        }

        //成功打开的时间方法
        void timer_Tick(object sender, EventArgs e)
        {
            this.Postionloading.ValueChanged -= new RoutedPropertyChangedEventHandler<double>(timelineSlider_ValueChanged);
            this.Postionloading.Value = this.video.Position.TotalSeconds / 10.0;
            this.Postionloading.ValueChanged += new RoutedPropertyChangedEventHandler<double>(timelineSlider_ValueChanged);
            try
            {
                playstart.Content = string.Format("{0}{1:00}:{2:00}:{3:00}", "", video.Position.Hours, video.Position.Minutes, video.Position.Seconds);
                playend.Content = string.Format("{0}{1:00}:{2:00}:{3:00}", "", video.NaturalDuration.TimeSpan.Hours, video.NaturalDuration.TimeSpan.Minutes, video.NaturalDuration.TimeSpan.Seconds);
            }
            catch { }
        }

        //播放进度，跳转到播放的哪个地方
        private void timelineSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            TimeSpan span = new TimeSpan(0, 0, (int)(Postionloading.Value) * 10);
            video.Position = span;
        }


        //窗口加载
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            inc.LoadingMain(mainbig);
            //登录成功播放声音
            inc.PlayerAudio(@"Start.wav ");

            //登录成功，读取用户设置的背景图片
            inc.SettingLoginUserBackground(Login.SEname, video);
            isPlaying = true;

            //设置用户头像
            //inc.SettingLoginUserPhoto(Login.SEname, ellipseWithImageBrush);

            //设置鼠标光标
            Cursor custoCursor = new Cursor(System.AppDomain.CurrentDomain.BaseDirectory + @"Cursor\Arrow.cur");
            this.Cursor = custoCursor;
            //StreamResourceInfo sri = Application.GetResourceStream(new Uri(@"Cursor\Arrow.cur", UriKind.Relative));
            //Cursor customCursor = new Cursor(sri.Stream);
            //this.Cursor = customCursor;

        }

        //显示或隐藏菜单栏
        private void ShowHiddenMenu()
        {
            //隐藏
            DoubleAnimation dfrom = new DoubleAnimation();
            dfrom.From = 160;
            dfrom.To = 0;
            dfrom.Duration = TimeSpan.FromSeconds(0.2);
            DoubleAnimation dfromo = new DoubleAnimation();
            dfromo.From = 1;
            dfromo.To = 0;
            dfromo.Duration = TimeSpan.FromSeconds(0.1);

            //显示
            DoubleAnimation dto = new DoubleAnimation();
            dto.From = 0;
            dto.To = 160;
            dto.Duration = TimeSpan.FromSeconds(0.2);
            DoubleAnimation dtoo = new DoubleAnimation();
            dtoo.From = 0;
            dtoo.To = 1;
            dtoo.Duration = TimeSpan.FromSeconds(0.1);


            //logo显示
            DoubleAnimation dalogo = new DoubleAnimation();
            dalogo.From = 0;
            dalogo.To = 160;
            dalogo.Duration = TimeSpan.FromSeconds(0.2);
            DoubleAnimation dalogoo = new DoubleAnimation();
            dalogoo.From = 0;
            dalogoo.To = 1;
            dalogoo.Duration = TimeSpan.FromSeconds(0.1);

            //logo隐藏
            DoubleAnimation dalogohid = new DoubleAnimation();
            dalogohid.From = 160;
            dalogohid.To = 0;
            dalogohid.Duration = TimeSpan.FromSeconds(0.2);
            DoubleAnimation dalogohido = new DoubleAnimation();
            dalogohido.From = 1;
            dalogohido.To = 0;
            dalogohido.Duration = TimeSpan.FromSeconds(0.1);

            if (menushow)
            {
                LeftMenu.BeginAnimation(FrameworkElement.WidthProperty, dfrom);
                LeftMenu.BeginAnimation(FrameworkElement.OpacityProperty, dfromo);
                test.Width = (System.Windows.GridLength.Auto);

                //logo
                logobg.BeginAnimation(FrameworkElement.WidthProperty, dalogohid);
                logobg.BeginAnimation(FrameworkElement.OpacityProperty, dalogohido);

                menushow = false;
            }
            else
            {
                LeftMenu.BeginAnimation(FrameworkElement.WidthProperty, dto);
                LeftMenu.BeginAnimation(FrameworkElement.OpacityProperty, dtoo);
                test.Width = System.Windows.GridLength.Auto;

                //logo
                logobg.BeginAnimation(FrameworkElement.WidthProperty, dalogo);
                logobg.BeginAnimation(FrameworkElement.OpacityProperty, dalogoo);


                menushow = true;
            }
        }

        //设置背景图片
        private void SettingBG()
        {
            //读取背景图片
            //string bgname = Encryption_Decryption.Decrypt(IniReadValue("VideoURL", "videourl", AppDomain.CurrentDomain.BaseDirectory + "\\Config.ini"));

            //应用背景
            //this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()
            //{
            //inc.SettingVideosbg(video, Login.Suser);
            //});

        }

        //读取INI文件方法
        static public string IniReadValue(string Section, string Key, string filepath)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, filepath);
            return temp.ToString();
        }

        //当鼠标移入到时间轴
        private void SliderTimer_MouseMove(object sender, MouseEventArgs e)
        {
            ShowTheOpacity(MovieControl);
            timervideocontrol.Interval = new TimeSpan(0, 0, 10);
            timervideocontrol.Tick += new EventHandler(timervideocontrol_Tick);

        }
        //当鼠标移出时间轴
        private void SliderTimer_MouseLeave(object sender, MouseEventArgs e)
        {
            timervideocontrol.Start();
        }


        //状态变量
        //播放器时间轴和按钮隐藏显示动画
        public void ShowTheOpacity(Grid _btn)
        {
            MovieControl.Visibility = System.Windows.Visibility.Visible;
            DoubleAnimation dashow = new DoubleAnimation();
            dashow.From = 0;
            dashow.To = 1;
            dashow.Duration = TimeSpan.FromSeconds(0.5);

            _btn.BeginAnimation(FrameworkElement.OpacityProperty, dashow);
        }
        public void HiddenTheOpacity(Grid _btn)
        {
            MovieControl.Visibility = System.Windows.Visibility.Hidden;
            DoubleAnimation dahide = new DoubleAnimation();
            dahide.From = 1;
            dahide.To = 0;
            dahide.Duration = TimeSpan.FromSeconds(0.5);
            _btn.BeginAnimation(FrameworkElement.OpacityProperty, dahide);
        }
        public void timervideocontrol_Tick(object sender, EventArgs e)
        {
            HiddenTheOpacity(MovieControl);
            timervideocontrol.Stop();
        }

        //显示内容区域动画
        public void ShowContentWin(Border _border)
        {
            DoubleAnimation dashowopacity = new DoubleAnimation();
            dashowopacity.From = 0;
            dashowopacity.To = 1;
            dashowopacity.Duration = TimeSpan.FromSeconds(1);
            _border.BeginAnimation(FrameworkElement.OpacityProperty, dashowopacity);
        }
        //隐藏内容区域动画
        public void HiddenContentWin(Border _border)
        {
            DoubleAnimation dahiddenapacity = new DoubleAnimation();
            dahiddenapacity.From = 1;
            dahiddenapacity.To = 0;
            dahiddenapacity.Duration = TimeSpan.FromSeconds(0.5);
            _border.BeginAnimation(FrameworkElement.OpacityProperty, dahiddenapacity);
        }
        //当播放视频结束
        private void video_MediaEnded(object sender, RoutedEventArgs e)
        {
            video.Stop();
            video.Play();
        }
    }
}
